home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’91 / DAL Files / DALtool 4⁄19 (System 6.x) / DalDalInterface.c < prev    next >
Text File  |  1991-06-19  |  7KB  |  336 lines

  1. /* DAL handling routines */
  2.  
  3. #include <string.h>
  4. #include "cl1api.h"
  5. #include "cl1err.h"
  6. #include "DalDemo.h"
  7.  
  8. extern Rect gOutRect;
  9. extern CharsHandle gInText;
  10. long    sessionId;
  11. long    gTimer;
  12. long    gCounter;
  13. int        gDALState;
  14. Boolean    gDALActive, gDALMonitor, gDALNewCodeFrag;
  15.  
  16. /***These first few routines provide basic utilities without relying upon
  17. outside libraries--trying to keep the executable small***/
  18.  
  19. /***StrLen***/
  20. int StrLen(s)
  21. char *s;
  22. {
  23.     int n;
  24.     
  25.     for (n = 0; *s != '\0'; s++) n++;
  26.     return n;
  27. }
  28.  
  29. /***StrToOut***/
  30. void StrToOut(demoWind,str)
  31. demoPeek    demoWind;
  32. char        *str;
  33. {
  34.     long    len;
  35.     
  36.     if ((len = (long) StrLen(str)) != 0)
  37.     {
  38.         TESetSelect(32767,32767,demoWind->outputTE);
  39.         TEInsert(str,len,demoWind->outputTE);
  40.     }
  41. }
  42.  
  43. /***IntToOut***/
  44. void IntToOut(demoWind,num)
  45. demoPeek demoWind;
  46. int    num;
  47. {
  48.     int i,j,c,sign;
  49.     char    str[24];
  50.     
  51.     if ((sign = num) < 0)
  52.         num = -num;
  53.     i = 0;
  54.     do {
  55.         str[i++] = num % 10 + '0';
  56.     } while ((num /= 10) > 0);
  57.     if (sign < 0)
  58.         str[i++] = '-';
  59.     str[i] = '\0';
  60.     for (i = 0, j = StrLen(str); i < j; i++, j--)
  61.         c = str[i], str[i] = str[j-1], str[j-1] = c;
  62.     StrToOut(demoWind,str);
  63. }
  64.  
  65. /***NLToOut***/
  66. void NLToOut(demoWind)
  67. demoPeek    demoWind;
  68. {
  69.     TEKey(TE_CARRIAGE_RETURN,demoWind->outputTE);    
  70. }
  71.  
  72. /***Provide a static method for displaying 'english' meaning to numeric err
  73. messages**/
  74.  
  75. void DALTransErr(demoWind,err)
  76. demoPeek demoWind;
  77. int    err;
  78. {
  79.     switch (err)
  80.     {
  81.         case A_OK:    StrToOut(demoWind,"OK/VAL ");
  82.                     break;
  83.         case A_NULL:    StrToOut(demoWind,"NULL ");
  84.                     break;
  85.         case A_ERROR:    StrToOut(demoWind,"ERROR ");
  86.                     break;
  87.         case A_READY:    StrToOut(demoWind,"READY ");
  88.                     break;
  89.         case A_BADTYPE:    StrToOut(demoWind,"BADTYPE ");
  90.                     break;
  91.         case A_BREAK:    StrToOut(demoWind,"BREAK ");
  92.                     break;
  93.         case A_EXEC:    StrToOut(demoWind,"EXEC ");
  94.                     break;
  95.         case A_NOTCONN:    StrToOut(demoWind,"NOTCONN ");
  96.                     break;
  97.         case A_SESSMAX:    StrToOut(demoWind,"SESSMAX ");
  98.                     break;
  99.         case A_INUSE:    StrToOut(demoWind,"INUSE ");
  100.                     break;
  101.         case A_NOHOST:    StrToOut(demoWind,"NOHOST ");
  102.                     break;
  103.         default:
  104.                 StrToOut(demoWind,"unknown error ");
  105.         }
  106. }
  107.         
  108. /***Create the session link***/
  109. void DALOpenLink(demoWind,node,user,pass)
  110. demoPeek demoWind;
  111. char *node, *user, *pass;
  112. {
  113.     int err;
  114.     
  115.     PtoCstr(node);
  116.     PtoCstr(user);
  117.     PtoCstr(pass);
  118.     sessionId = -1L;
  119.     
  120.     err = CLInit(&sessionId,node,user,pass,"\0");
  121.     if (err != A_OK)
  122.         DALErrorHandler(demoWind,err);
  123.     else
  124.     {
  125.         gDALActive = TRUE;
  126.         StrToOut(demoWind,"OpenLink:Connection established");
  127.         NLToOut(demoWind);
  128.     }
  129. }
  130.  
  131. /***DALSendExecute will send the fragment found in the input buffer
  132. and instruct the server to execute it if no errors occur during transmission***/
  133.  
  134. void DALSendExecute(demoWind)
  135. demoPeek demoWind;
  136. {
  137.     int    len;
  138.     int err;
  139.     TEPtr    te;
  140.     
  141.     te = *(demoWind->curTE);
  142.     if ((len = te->teLength) == 0) return;
  143.  
  144.     SwitchToNewArea(demoWind);    
  145.     NLToOut(demoWind);
  146.     StrToOut(demoWind,"<Sending and Executing>");
  147.     NLToOut(demoWind);
  148.     TEPaste(demoWind->curTE);
  149.     NLToOut(demoWind);
  150.     AdjustScrollBar(demoWind);
  151.     SwitchToNewArea(demoWind);
  152.     gDALNewCodeFrag = TRUE;
  153.         
  154.     HLock(gInText);
  155.     err = CLSend(sessionId,*gInText,len);
  156.     HUnlock(gInText);
  157.     if (err != A_OK)
  158.         DALErrorHandler(demoWind,err);
  159.     else
  160.     {
  161.         err = CLExec(sessionId);
  162.         if (err != A_OK)
  163.             DALErrorHandler(demoWind,err);
  164.     }    
  165. }
  166.  
  167. /***DALMonitorState is used to find the server state on a periodic basis***/
  168. void DALMonitorState(demoWind)
  169. demoPeek demoWind;
  170. {
  171.     int    err;
  172.     long ticks;
  173.     
  174.     if ((ticks = TickCount()) >= gTimer)
  175.     {
  176.         gTimer += DAL_MONITOR_INTERVAL;
  177.         ++gCounter;
  178.         err = CLState(sessionId);
  179.         if (err == A_ERROR && gDALNewCodeFrag)
  180.             DALErrorHandler(demoWind,err);
  181.  
  182.         if (gDALMonitor) DALDisplayState(demoWind);
  183.         if (gDALState == A_VALUE && gDALNewCodeFrag) DALReadLink(demoWind);
  184.     }
  185. }    
  186.  
  187. /***DALErrorHandler gets additional error status messages from server***/
  188. void DALErrorHandler(demoWind,err)
  189. demoPeek demoWind;
  190. int err;
  191. {
  192.     long    errp;
  193.     long    err2p;
  194.     char    itm1[80];
  195.     char    itm2[80];
  196.     char    msg[80];
  197.     
  198.     gDALState = CLGetErr(sessionId, &errp, &err2p, itm1, itm2, msg);
  199.     NLToOut(demoWind);
  200.     DALTransErr(demoWind,err);
  201.     StrToOut(demoWind,msg);
  202.     NLToOut(demoWind);
  203.     gDALNewCodeFrag = FALSE;
  204. }
  205.  
  206. /***DALDisplayState***/
  207. void DALDisplayState(demoWind)
  208. demoPeek demoWind;
  209. {
  210.         DALTransErr(demoWind,gDALState);
  211. }
  212.  
  213. /***DALCloseSession does a clean shutdown***/
  214. void DALCloseLink(demoWind)
  215. demoPeek demoWind;
  216. {
  217.     int err;
  218.     
  219.     err = CLEnd(sessionId);
  220.     if (err != A_OK)
  221.         DALErrorHandler(demoWind,err);
  222.     else
  223.         gDALActive = FALSE;
  224. }
  225.  
  226. /***DALReadLink gets data from the server when ready***/
  227. void DALReadLink(demoWind)
  228. demoPeek    demoWind;
  229. {
  230.     short    typep;
  231.     short    lenp;
  232.     short    placep;
  233.     short    flagsp;
  234.     Ptr        buffer[DAL_MAXCOL];
  235.     int     err;
  236.     short    maxCol;
  237.     short    bIndex;
  238.     Boolean    firstRow;
  239.     
  240. /* do informational wait loop checking for data */
  241.  
  242.     firstRow = TRUE;
  243.     bIndex = 0;
  244.     maxCol = 0;
  245.     buffer[bIndex] = (Ptr) NIL;
  246.  
  247.     typep = A_ANYTYPE;
  248.     err = CLGetItem(sessionId, DAL_MONITOR_INTERVAL/4, &typep, &lenp, &placep, &flagsp,
  249.                     NIL);
  250.     if (err < A_VALUE) return;
  251.  
  252.     StrToOut(demoWind,"<Reading Program Output>");
  253.     NLToOut(demoWind);
  254.     
  255.     while (err != A_ERROR && err != A_READY)
  256.     {
  257.         typep = A_ANYTYPE;
  258.         err = CLGetItem(sessionId, AW_FOREVER, &typep, &lenp, &placep, &flagsp,
  259.                         NIL);
  260.         if (err != A_VALUE) break;
  261.     /* if this is the first row, set up buffer space for each column's data */
  262.         if (firstRow)
  263.         {
  264.             switch (typep)
  265.             {
  266.                 case A_BOOLEAN:
  267.                 case A_SMINT:
  268.                 case A_INTEGER:
  269.                 case A_SMFLOAT:
  270.                 case A_DATE:
  271.                 case A_TIME:
  272.                 case A_TIMESTAMP:
  273.                         lenp = 8;
  274.                         break;
  275.                 case A_VCHAR:
  276.                 case A_CHAR:
  277.                         lenp += 1;    /* add one for null character termination */
  278.                         break;
  279.                 default:
  280.                         break;
  281.             }
  282.             if ((buffer[bIndex] = NewPtr(lenp)) == NIL) ErrorHandler(NO_MEMORY);
  283.             /* check to see if this is the last colitem in this row...
  284.                 last time to grab pointers for this data request */
  285.             firstRow = !(flagsp & AF_RECEND);
  286.         }
  287.         else
  288.         {
  289.             switch (typep)
  290.             {
  291.                 case A_VCHAR:
  292.                 case A_CHAR:
  293.                         DisposPtr(buffer[bIndex]);
  294.                         lenp += 1;    /* add one for null character termination */
  295.                         if ((buffer[bIndex] = NewPtr(lenp)) == NIL) ErrorHandler(NO_MEMORY);
  296.                         break;
  297.                 default:
  298.                         break;
  299.             }
  300.         } /*else*/
  301.         if (bIndex > maxCol) maxCol = bIndex;
  302.         err = CLGetItem(sessionId, AW_FOREVER, &typep, &lenp, &placep, &flagsp,
  303.                         buffer[bIndex]);        
  304.         if (err != A_VALUE && err != A_NULL) break;
  305.         if (flagsp & AF_RECEND)
  306.         {
  307.             DALDisplayRow(demoWind,buffer,bIndex);
  308.             bIndex = 0;
  309.         }
  310.         else
  311.             ++bIndex;
  312.     } /*while*/
  313.     /* free up memory */
  314.     bIndex = 0;
  315.     while (maxCol-- >= 0)
  316.         DisposPtr(buffer[bIndex++]);
  317.     NLToOut(demoWind);
  318.  
  319. } /*DALReadLink*/
  320.  
  321. /***DALDisplayRow***/
  322. void DALDisplayRow(demoWind,buffer,bufLen)
  323. demoPeek demoWind;
  324. Ptr *buffer;
  325. int bufLen;
  326. {
  327.     int    i;
  328.     
  329.     for (i = 0; i <= bufLen; i++)
  330.     {
  331.         StrToOut(demoWind,(char *) buffer[i]);
  332.         StrToOut(demoWind," ");
  333.     }
  334.     
  335.     NLToOut(demoWind);
  336. }